package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.result.Result;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * @author liuyp
 * @since 2023/09/16
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public Result businessData() {
        LocalDateTime startOfToday = LocalDateTime.now().with(LocalTime.MIN);
        LocalDateTime endOfToday = LocalDateTime.now().with(LocalTime.MAX);

        BusinessDataVO vo = new BusinessDataVO();
        //1. 今日新增用户数量
        Integer newUser = userMapper.countByDate(startOfToday, endOfToday);
        vo.setNewUsers(newUser);

        //2. 今日有效订单数
        Integer validOrderCount = orderMapper.countStatistics(startOfToday, endOfToday, Orders.COMPLETED);
        vo.setValidOrderCount(validOrderCount);

        //3. 今日订单完成率
        Integer orderTotalCount = orderMapper.countStatistics(startOfToday, endOfToday, null);
        vo.setOrderCompletionRate(0D);
        if (orderTotalCount!=0) {
            vo.setOrderCompletionRate(1.0 * validOrderCount / orderTotalCount);
        }

        //4. 今日营业额
        Double turnover = orderMapper.sumAmountByDate(startOfToday, endOfToday);
        if (turnover == null) {
            turnover = 0D;
        }
        vo.setTurnover(turnover);

        //5. 今日平均客单价
        vo.setUnitPrice(0D);
        if (validOrderCount!=0) {
            vo.setUnitPrice(1.0 * turnover / validOrderCount);
        }

        return Result.success(vo);
    }

    @Override
    public Result overviewOrders() {
        LocalDateTime startOfToday = LocalDateTime.now().with(LocalTime.MIN);
        LocalDateTime endOfToday = LocalDateTime.now().with(LocalTime.MAX);

        OrderOverViewVO vo = new OrderOverViewVO();
        //今日订单总数
        Integer allOrders = orderMapper.countStatistics(startOfToday, endOfToday, null);
        vo.setAllOrders(allOrders);

        //今日已取消订单数量
        Integer canceledOrders = orderMapper.countStatistics(startOfToday, endOfToday, Orders.CANCELLED);
        vo.setCancelledOrders(canceledOrders);

        //今日已完成订单数量
        Integer completedOrders = orderMapper.countStatistics(startOfToday, endOfToday, Orders.COMPLETED);
        vo.setCompletedOrders(completedOrders);

        //今日待派送数量
        Integer deliveredOrders = orderMapper.countStatistics(startOfToday, endOfToday, Orders.CONFIRMED);
        vo.setDeliveredOrders(deliveredOrders);

        //今日待接单数量
        Integer waitingOrders = orderMapper.countStatistics(startOfToday, endOfToday, Orders.TO_BE_CONFIRMED);
        vo.setWaitingOrders(waitingOrders);

        return Result.success(vo);
    }

    @Override
    public Result overviewDishes() {
        DishOverViewVO vo = new DishOverViewVO();

        // 已停售菜品数量
        Integer discontined = dishMapper.countByStatus(StatusConstant.DISABLE);
        vo.setDiscontinued(discontined);

        // 已启售菜品数量
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        vo.setSold(sold);

        return Result.success(vo);
    }

    @Override
    public Result overviewSetmeals() {
        SetmealOverViewVO vo = new SetmealOverViewVO();

        //已停售套餐数量
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);
        vo.setDiscontinued(discontinued);

        //已启售套餐数量
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);
        vo.setSold(sold);

        return Result.success(vo);
    }
}